﻿using System.Windows.Forms;
using ComputerVision.BaseForm;
using ComputerVision.Data;
using ComputerVision.Services;
using Autofac;
using ComputerVision.Data.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ComputerVision.LoaiCoSoForm
{
    public partial class frmBaoCaoThietBi : frmList
    {
        private IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;
        private readonly IService<LoaiCoSo> _loaiCoSo;
        private readonly IService<Nganh> _nganh;
        private readonly IService<QuanHuyen> _quanHuyen;
        private readonly IService<ChuyenDe> _chuyenDe;

        public frmBaoCaoThietBi()
        {
            InitializeComponent();

            report.AutoGenerateColumns = false;
            report.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.Columns[3].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            report.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;

            report.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _quanHuyen = Program.Container.Resolve<IService<QuanHuyen>>();
            _chuyenDe = Program.Container.Resolve<IService<ChuyenDe>>();

            LoadQuanHuyen();
            LoadChuyenDe();
            LoadLoaiCoSo();
            LoadNganh();
        }

        private void GenerateReport(object sender, System.EventArgs e)
        {
            BuildReportData();  
        }

        public void LoadLoaiCoSo()
        {
            var loaiCoSo = _loaiCoSo.ToList().OrderBy(c => c.Ten).ToList();
            loaiCoSo.Insert(0, new LoaiCoSo());
            drlLoaiCoSo.DataSource = loaiCoSo;
            drlLoaiCoSo.DisplayMember = "Ten";
            drlLoaiCoSo.ValueMember = "Id";
        }

        public void LoadQuanHuyen()
        {
            var quanHuyen = _quanHuyen.ToList().OrderBy(q => q.Ten).ToList();
            var toanTinh = new QuanHuyen();
            toanTinh.Ten = "Toàn tỉnh";
            quanHuyen.Insert(0, toanTinh);
            drlQuanHuyen.DataSource = quanHuyen;
            drlQuanHuyen.DisplayMember = "Ten";
            drlQuanHuyen.ValueMember = "Id";
        }

        public void LoadNganh()
        {
            var nganh = _nganh.ToList().OrderBy(n => n.Ten).ToList();
            nganh.Insert(0, new Nganh());
            drlNganhNghe.DataSource = nganh;
            drlNganhNghe.DisplayMember = "Ten";
            drlNganhNghe.ValueMember = "Id";
        }

        public void LoadChuyenDe()
        {
            var chuyenDe = _chuyenDe.ToList().OrderBy(h => h.Ten).ToList();
            chuyenDe.Insert(0, new ChuyenDe());
            drlChuyenDe.DataSource = chuyenDe;
            drlChuyenDe.DisplayMember = "Ten";
            drlChuyenDe.ValueMember = "Id";
        }

        private void BuildReportData()
        {
            var results = GetData();
                        
            int numberItem = results.Count;
            if (numberItem == 0)
            {
                report.DataSource = null;
                return;
            }

            string thongTin = "Thông tin cơ bản:{0}- Quận/Huyện: {1}{0}- Loại cơ sở: {2}{0}- Ngành/Nghề: {3}{0}- Chuyên đề: {4}";

            progressBar.Value = 1;
            progressBar.Maximum = numberItem;

            if (results.Count > 0)
            {
                var items = new List<ReportPhuongTien>();

                foreach (var phieu in results)
                {
                    string quan = phieu.QuanHuyenId.HasValue ? Convert.ToString(phieu.QuanHuyen.Ten) : string.Empty;
                    string loaiCoSo = phieu.LoaiCoSo.Ten;
                    string nganh = phieu.Nganh.Ten;
                    string chuyenDe = phieu.Nganh.ChuyenDeId.HasValue ? phieu.Nganh.ChuyenDe.Ten : string.Empty;

                    ReportPhuongTien item = new ReportPhuongTien();

                    item.TenCoSo = phieu.TenCoSo;

                    item.ThongTinCoBan = string.Format(thongTin, Environment.NewLine,
                        quan, loaiCoSo, nganh, chuyenDe);

                    item.PhuongTien = GetPhuongTienReport(phieu.PhuongTienCoSo);

                    items.Add(item);
                    progressBar.Increment(1);
                }

                report.DataSource = items;
            }
        }

        private List<PhieuDieuTraCoSo> GetData()
        {
            var quan = drlQuanHuyen.SelectedItem as QuanHuyen;
            var loaiCoSo = drlLoaiCoSo.SelectedItem as LoaiCoSo;
            var nganh = drlNganhNghe.SelectedItem as Nganh;
            var chuyenDe = drlChuyenDe.SelectedItem as ChuyenDe;

            List<PhieuDieuTraCoSo> results = new List<PhieuDieuTraCoSo>();

            if (string.Equals(quan.Ten, "Toàn tỉnh", StringComparison.InvariantCultureIgnoreCase))
            {
                results = _phieuDieuTraCoSo.ToList().Where(p => p.QuanHuyenId.HasValue).ToList();
            }
            else
            {
                results = _phieuDieuTraCoSo.ToList().Where(p => p.QuanHuyenId.HasValue && p.QuanHuyenId.Value == quan.Id).ToList();
            }

            if (!string.IsNullOrEmpty(loaiCoSo.Ten))
            {
                results = results.Where(p => p.LoaiCoSoId.Value == loaiCoSo.Id).ToList();
            }

            if (!string.IsNullOrEmpty(nganh.Ten))
            {
                results = results.Where(p => p.NganhId.Value == nganh.Id).ToList();
            }

            if (!string.IsNullOrEmpty(chuyenDe.Ten))
            {
                results = results.Where(p => p.NganhId.HasValue && p.Nganh.ChuyenDeId.HasValue && p.Nganh.ChuyenDeId == chuyenDe.Id).ToList();
            }

            return results;
        }

        private string GetPhuongTienReport(ICollection<PhuongTienCoSo> list)
        {
            StringBuilder content = new StringBuilder();

            if (list.Count == 0) return "Cơ sở không có phương tiện PCCC nào.";

            content.Append("Danh sách PT PCCC");
            content.Append(Environment.NewLine);
            string template = "- Tên: {0}, số lượng: {1}{2}";

            foreach (var pt in list)
            {
                content.Append(string.Format(template, pt.Ten, pt.SoLuong, Environment.NewLine));
            }

            return content.ToString();
        }
    }
}
